Skip to content

Stabilize Poisson probabilities, log masses, and inverse-CDF searches - #153

Merged
jmh530 merged 10 commits into
libmir:masterfrom
jmh530:codex/poisson-numerical-review
Oct 11, 2026
Merged

jmh530 merged 10 commits into
libmir:masterfrom
jmh530:codex/poisson-numerical-review

Conversation

@jmh530

@jmh530 jmh530 commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix Poisson calculations that lose representable probabilities through intermediate overflow, underflow, or cancellation, and replace the inverse-CDF search with bounded bracketing and bisection.

For example, the direct PMF at count and rate 200 previously returned NaN instead of approximately 0.0281977. The direct CDF at count and rate 1000 also overflowed, causing the median quantile to return 999 instead of 1000. Small upper tails could disappear when calculated as 1 - CDF.

Changes

  • Preserve ordinary direct PMF and CDF calculations, with fallbacks when intermediate values lose range. Bound factorial and summation work and prevent count increments from wrapping at size_t.max.
  • Evaluate small upper tails directly and fall back from gamma-based PMF subtraction when adjacent CDF values are too close.
  • Center large-count log PMFs around the rate to avoid subtracting large, nearly equal terms. Preserve the original result-type calculation for counts below 32.
  • Retain the exponential range in fp_poissonPMF by halving the rate before evaluating the exponential and squaring in extended storage. Document that the exponential seed still has double precision.
  • Bracket and bisect inverse-CDF results using the smaller probability tail. Define count-quantile endpoints as zero at p = 0 and size_t.max at p = 1, also using size_t.max for results outside the return type's range. Gamma rate inversion retains its distinct endpoint interpretation.
  • Correct the binomial Poisson-approximation example and test both sides of its quantile boundary.

Validation

  • Independent 100-digit mpmath references for probabilities and quantiles, plus a 108-case log-mass comparison covering branch boundaries and counts up to 10¹².
  • Regression tests for float, double, real, and extended storage, including subnormal probabilities, infinite rates, maximum counts, and inverse-CDF neighboring-bin conditions.
  • All 58 modules passed the full Windows LDC suite. Focused optimized Poisson/binomial tests passed on Windows DMD/LDC and Linux DMD/LDC.
  • Paired LDC benchmarks found modest ordinary-path overhead and faster inverse searches for the sampled inputs. The initial Linux small-count log-PMF slowdown was removed by retaining its original arithmetic. These measurements are specific to the tested workloads.

Incomplete-gamma accuracy and cost remain dependent on Phobos. The normal approximation algorithms are not redesigned.

AI-Assisted-By: Codex (GPT-6 Astra Medium)

Retain the direct formula when the exponential factor and computed mass are normal and finite; otherwise use a real-valued logarithmic fallback. Bound factorial work for large counts and avoid wrapping the factorial argument at size_t.max.

Add float, double, and real regressions for central overflow, infinite rates, maximum counts, and a subnormal tail probability.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Fall back to incomplete gamma when the direct CDF sum loses range or precision, and bound long direct sums. Convert counts before adding one in gamma calls so size_t.max cannot wrap.

Preserve ordinary CCDF subtraction when the remaining tail is at least one eighth; evaluate smaller tails directly with incomplete gamma. Cover central overflow, small upper tails, infinite rates, and maximum counts across float, double, and real.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Use the direct PMF fallback when adjacent complemented incomplete gamma values are too close for reliable subtraction. Convert counts before adding one, and preserve the existing zero-event path.

Cover representable upper-tail masses, maximum counts, and agreement across the subtraction and fallback regions for float, double, and real.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Bracket and bisect the count using the smaller probability tail instead of accumulating PMFs. This avoids stalled sums, unsigned downward-loop wraparound, and incorrect results after CDF overflow.

Use size_t.max for unbounded or unrepresentable count quantiles, consistent with negative binomial inversion. Handle both endpoints explicitly in count approximations and gamma rate inversion, and convert before incrementing the gamma shape.

Add independent quantile references and float, double, and real endpoint and neighboring-bin checks, including probabilities immediately below one.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Halve large rates before evaluating the double-precision exponential, then square in Fp storage. This prevents an underflowed seed from discarding an otherwise representable mass or extended-range zero-event probability.

Keep the ordinary exponential path for rates through 512, document its precision limit, and cover central recovery and both sides of the scaling boundaries.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Evaluate large-count log masses with Stirling's correction and a centered deviance term. Near the mean, use a convergent series instead of subtracting large nearly equal logarithmic terms. Reuse this calculation in the PMF fallback and public log PMF.

Handle zero counts and infinite rates explicitly, avoid maximum-count factorial overflow, and add independent central references and branch-boundary checks.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
The Poisson CDF at count and rate 10000 is approximately 0.5026595812190076, so probability 0.5026596 requires count 10001. Check both sides of this independently verified boundary instead of preserving the old inverse-search result.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Keep the original result-type logarithm and factorial evaluation below count 32, with explicit zero-count and infinite-rate handling. Reserve the centered real-precision expansion for larger counts.

This removes the measured Linux LDC slowdown from promoting every small-count log mass to real while retaining the numerical fixes in the large-count path.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Describe both uses of poissonLogMass: the PMF fallback and large-count
LPMF evaluation. Remove the outdated statement that it runs only when
direct intermediates fail.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
@codecov

codecov Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Add regressions for zero-event exponential underflow, an overflowing
count-to-rate ratio with a finite log mass, and extended rates beyond
the double exponential seed's range.

Check finite rates whose quantiles exceed size_t.max and probabilities
just above the normal approximation's lower cutoff, where rounding can
produce a nonpositive continuity-corrected result.

Exercise the six branches reported as uncovered without changing
production behavior.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
@jmh530
jmh530 merged commit 2404715 into libmir:master Oct 11, 2026
24 checks passed
@jmh530
jmh530 deleted the codex/poisson-numerical-review branch October 11, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant